package com.example.easyexcel.dangwen.handler;

import com.alibaba.fastjson.JSON;
import com.example.easyexcel.dangwen.model.Model1;
import com.example.easyexcel.model.UserModel;
import com.example.easyexcel.strategy.CustomCellStyleStrategy;
import com.example.easyexcel.strategy.CustomColumnWidthStrategy;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static com.alibaba.excel.EasyExcelFactory.write;

/**
 * @author dangwen1
 * @date 2022年01月19日 9:54
 * @description
 */
public class TxtHandler {
    public static void main(String[] args) throws Exception {
        File file = new File("D:\\dangwen1\\Desktop\\chuliwenjian.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"gbk");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;//代表一整行字符串
        int counter = 1;
        List<Model1> model1s = new ArrayList<>();
        Model1 model1 = new Model1();
        while((line = bufferedReader.readLine()) != null){
            if (line.equals(counter+"")) {
                model1 = new Model1();
                model1s.add(model1);
                counter++;
            }
            if (line.startsWith("【标题】")) {
                int i = line.indexOf(":");
                String substring = line.substring(i + 1);
                model1.setTitle(substring);
            }
            if (line.startsWith("【作者】")) {
                int i = line.indexOf(":");
                String substring = line.substring(i + 1);
                model1.setAuthor(substring);
            }
            if (line.startsWith("【作者单位】")) {
                int i = line.indexOf(":");
                String substring = line.substring(i + 1);
                String[] deps = substring.split(";");
                for (String depStr : deps) {
                    int indexR = depStr.indexOf(")");
                    String removeQuotaStr = depStr.substring(indexR + 1);
                    String[] split = removeQuotaStr.split(",");
                    String unit = split[0];
                    model1.setUnit(unit);
                    if (split.length > 1) {
                        String[] unitCityStr = split[1].split(" ");
                        String unitCity = unitCityStr[0];
                        String postCode = unitCityStr[1];
                        model1.setUnitCity(StringUtils.isEmpty(unitCity) ? "--" : unitCity);
                        model1.setPostCode(postCode);
//                        System.out.println("unit:" + unit + "---unitCity:" + unitCity + "---postCode:" + postCode);
                    }
                }
            }
            if (line.startsWith("【出处】")) {
                int i = line.indexOf(":");
                String substring = line.substring(i + 1);
                String[] outs = substring.split(";");
                String ref = outs[0];
                String year = outs[1];
                String out = outs[2];
                String[] split = out.split(":");
                String periodical = split[0];
                String pageRange = split[1];
                model1.setReference(ref);
                model1.setYear(year);
                model1.setPeriodical(periodical);
                model1.setPageRange(pageRange);
            }
        }
        bufferedReader.close();
        String fileName = "D:\\dangwen1\\Desktop\\chuliwenjian.xlsx";
        write(fileName, Model1.class).sheet().head(Model1.class)
                .registerWriteHandler(new CustomColumnWidthStrategy())
                .registerWriteHandler(new CustomCellStyleStrategy()).doWrite(model1s);
    }
}
